<?php
class Site{
	var $nom;
	var $lien;
	var $categorie;

	public function __get($param){return $this->param;}
	public function __set($param, $value){$this->param = $value;}

	public function __construct($nom="", $lien="", $categorie=""){
		$this->nom = $nom;
		$this->lien = $lien;
		$this->categorie = $categorie;
	}
	
	public function __tostring(){return "<a href='" . $this->lien . "' target='_blank'>" . $this->nom . "</a>";}
}

class Categorie{
	var $tb;//list de Site
	var $nb;//length de tb
	var $cat;//la categorie
	
	public function __get($param){return $this->param;}
	public function __set($param, $value){$this->param = $value;}
	
	public function __construct(){
		$this->tb = "";
		$this->nb = 0;
		$this->cat = "";
	}
	
	public function __add($s){
		if ($this->cat == ""){
			$this->cat = $s->categorie;
			$this->tb[$this->nb] = $s;
			$this->nb++;
                        return true;
		}
		else{
			if($this->cat == $s->categorie){
				$this->tb[$this->nb] = $s;
				$this->nb++;
                                return true;
			}
		}
                return false;
	}
	
	public function __tostring(){
		$temp = "<p><span class='Titre'>" . $this->cat . " :</span><br/>";
		for($a = 0;$a<$this->nb;$a++){
			if($this->nb == ($a+1)){
				$temp = $temp . $this->tb[$a]->__tostring() . "</p>";
			}
			else{
				$temp = $temp . $this->tb[$a]->__tostring() . "<br/>";
			}
		}
		return $temp;
	}
}

class Categories{
	var $tb;//list de Categorie
	var $nb;//length de tb
	
	public function __get($param){return $this->param;}
	public function __set($param, $value){$this->param = $value;}
	
	public function __construct(){
		$this->tb = array();
		$this->nb = 0;
	}
	
	public function __add($s){
		$ajout = false;
		$fin = false;
		$index = 0;
		while($index < $this->nb && !$ajout)
  		{
  			$ajout = $this->tb[$index]->__add($s);
  			$index++;
  		}
		if(!$ajout){
			$temp = new Categorie();
			$temp->__add($s);
			$this->tb[$this->nb] = $temp;
			$this->nb++;
		}
	}
}

class Ligne{
	var $tb;
	
	public function __construct($l){
		$this->tb = $l;
	}
	
	public function __add($s){
		$this->tb[count($this->tb)] = $s;
	}
	
	public function __mystring($index){
		$temp = "";
		if($index == 0){
			for($a = 0;$a<count($this->tb);$a++){$temp = $temp . "<th>" . $this->tb[$a] . "</th>";}
		}
		else{
			for($a = 0;$a<count($this->tb);$a++){$temp = $temp . "<td>" . $this->tb[$a] . "</td>";}
		}
		return $temp;
	}
}

class Tableau{
	var $tbs;
	var $border;
	var $id;
	var $objet;
	var $ajout;
	var $supprime;
	var $misajour;
	
	public function __construct($border, $id, $objet){
		$this->tbs = array();
		$this->border = $border;
		$this->id = $id;
		$this->objet = $objet;
		$this->ajout = false;
		$this->supprime = false;
		$this->misajour = false;
	}
	
	public function __add($s){$this->tbs[count($this->tbs)] = $s;}
	
	public function __getobjet(){return $this->objet;}
	public function __setadd($val){$this->ajout = $val;}
	public function __setdel($val){$this->supprime = $val;}
	public function __setupdate($val){$this->misajour = $val;}
	public function __getjavaval(){
		$temp = "var def" . $this->id . " = [";
		foreach ($this->objet as $obj){
			if(get_class($obj) == "Selection"){
				$temp .= $obj->__getjavaarray() . ",";
			}
			else if(get_class($obj) == "Bouton"){$temp .= "'btn',";}
			else{
				$temp .= "'txt',";
			}
		}
		return substr($temp, 0, -1) . "]; var etat" . $this->id . " = [];";
	}
	
	public function __tostring(){
		$temp = "<table border='" . $this->border . "' id='" . $this->id . "'><thead><tr>";
		$n = 0;
		foreach ($this->tbs as $tb){
			if($n == 0){
				$temp .= $tb->__mystring($n) . ($this->supprime ? "<th></th>" : "") . ($this->misajour ? "<th></th>" : "") . "</tr></thead><tbody>";
			}
			else{
				$option = "";
				if($this->misajour){
					$btnupdate = new Bouton("", "Update");
					$btnupdate->__addAction("btnupdate('" .$this->id . "', " . count($this->objet) . ", " . $n . ");");
					$option .=  "<td>" . $btnupdate->__tostring() . "</td>";
				}
				if($this->supprime){
					$btndelete = new Bouton('delete' . $this->id . $n, "Delete");
					$btndelete->__addAction("btndelete('" . $this->id . "', " . count($this->objet) . ", " . $n . ");");
					$option .= "<td>" . $btndelete->__tostring() . "</td>";
				}
				if(!$this->supprime && !$this->misajour && $ajout){
					$option .= "<td></td>";
				}
				$temp .= "<tr>" . $tb->__mystring($n) . $option . "</tr>";
			}
			$n++;
		}
		if($this->ajout){
			$temp .= "<tr>";
			$n = 1;
			foreach ($this->objet as $obj){
				if(get_class($obj) == "Selection"){
					$obj->id = "txtadd" . $this->id . $n;
					$temp .= "<td>" . $obj->__tostring() . "</td>";
					$n++;
				}
				else if(get_class($obj) == "Bouton"){
					$temp .= "<td></td>";
				}
				else{
					$temp .= "<td><input type='text' id='txtadd" . $this->id . $n . "'/></td>";
					$n++;
				}
			}
			$btninsert = new Bouton("", "Insert");
			$btninsert->__addAction("btnadd('" . $this->id . "'," . count($this->objet) . ");");
			$temp .= "<td>" . $btninsert->__tostring() . "</td>" . ($this->misajour && $this->supprime ? "<td></td>" : "") . "</tr>";
		}
		$temp = $temp . "</tbody></table>";
		return $temp;
	}
}

class Selection{
	var $tb;
	var $id;
	var $selected;
	
	public function __construct($id, $l){
		$this->tb = $l;
		$this->selected = -1;
		$this->id = $id;
	}

	public function __add($option, $selected){
		$this->tb[count($this->tb)] = $option;
		if($selected){$this->selected = count($this->tb);}
	}
	
	public function __getjavaarray(){
		$temp = "[";
		for($a = 0;$a<count($this->tb);$a++){
			$temp = $temp . '"' . $this->tb[$a] . '",';
		}
		return substr($temp, 0, -1) . "]"; 
	}
	
	public function __doselected($index){$this->selected = $index;}
	
	public function __tostring(){
		$temp = "<select id='" . $this->id . "'>";
		for($a = 0;$a<count($this->tb);$a++){
			$temp .= "<option value='" . $this->tb[$a] . ($this->selected == $a ? "' selected>" : "'>") . $this->tb[$a] . "</option>";
		}
		$temp = $temp . "</select>";
		return $temp;
	}
}

class Bouton{
	var $id;
	var $value;
	var $action;
	
	public function __construct($id, $value){
		$this->id = $id;
		$this->value = $value;
		$this->action = "";
	}
	public function __addAction($act){
		$this->action .= $act;
	}
	public function __tostring(){
		return "<input type='button'" . ($this->id == "" ? "" : " id='" . $this->id . "'") . " value='" . $this->value . "' onclick=" . '"' . $this->action . '"/>';
	}
}
?>